<?php
    /**
     * Google Chart helper
     * 
     * @author Anton Perkin
     * @version 1.0
     *
     */
    class GoogleChart
    {
        /* VARIABLES */

    	/**
    	 * Chart title
    	 *
    	 * @var string
    	 */
    	public $title;

    	/**
    	 * Chart container id
    	 *
    	 * @var string
    	 */
    	public $containerId = 'chartContainer';

    	/**
    	 * Chart width
    	 *
    	 * @var int
    	 */
    	public $width = 400;

    	/**
    	 * Chart height
    	 *
    	 * @var int
    	 */
    	public $height = 240;

    	/**
    	 * Horizontal asix label
    	 *
    	 * @var string
    	 */
    	public $hAsixLabel;

    	/**
    	 * Horizontal asix label style
    	 *
    	 * @var string
    	 */
    	public $hAsixLabelStyle = "color: 'red'";

    	/**
    	 * Columns
    	 *
    	 * @var array
    	 */
    	public $columns;

    	/**
    	 * Rows
    	 *
    	 * @var array
    	 */
    	public $rows;
		

        /* CONSTRUCTORS */

        /**
         * Default constructor
         */
    	function __construct ()
    	{

    	}

    	
    	/* PRIVATE METHODS */
    	
        /**
    	 * Chart initialization
    	 */
    	private function InitializeChart()
    	{
    		//library
    	    echo '<script type="text/javascript" src="https://www.google.com/jsapi"></script>';
    		
    	    //initial
    	    $jsCode = "
		    <script type='text/javascript'>
		    	google.load('visualization', '1', {packages:['corechart']});
		      	google.setOnLoadCallback(drawChart);
		      	function drawChart() 
		      	{
			        var data = new google.visualization.DataTable();
			";
    	    
    	    //rows number
    	    $jsCode .= "data.addRows(".count($this->rows).");\n";
    	    
    	    //columns
    	    foreach ($this->columns as $column)
    	    {
    	    	$jsCode .= "data.addColumn('{$column['type']}', '{$column['title']}');\n";
    	    }
    	    
    	    //values
    	    foreach ($this->rows as $rowNumber => $row)
    	    {
    	    	foreach ($row as $index => $value)
    	    	{
    	    		$jsCode .= "data.setValue($rowNumber, $index, $value);";
    	    	}
    	    }    	    
    	    
    	    //draw
    	    $jsCode .= " 
			        var chart = new google.visualization.ColumnChart(document.getElementById('{$this->containerId}'));
			        chart.draw(data, {width: {$this->width}, height: {$this->height}, title: '{$this->title}',
		                          hAxis: {title: '{$this->hAsixLabel}', titleTextStyle: {".$this->hAsixLabelStyle."}}
					});
      			}
    		</script>";
    	    
    	    echo $jsCode;
    	}
    	
    	
    	/* PUBLIC METHODS */

    	/**
    	 * Adds a column
    	 *
    	 * @param array $column column
    	 */
		public function AddColumn($column)
		{
			$this->columns[] = $column;
		}  

    	/**
    	 * Adds a row
    	 *
    	 * @param array $row row
    	 */
		public function AddRow($row)
		{
			$this->rows[] = $row;
		}  
		
    	/**
    	 * Displays the chart
    	 *
    	 */
    	public function DisplayChart()
    	{
		    $this->InitializeChart();
    		
		    //map container
		    echo '<div id="'.$this->containerId.'" style="'.$this->containerStyle.'">&nbsp;</div>';
    	}
    }
?>